package com.gblfy.controller;

import com.gblfy.service.IFlowService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gblfy
 * @ClassNme FlowController
 * @Description TODO
 * @Date 2019/11/4 13:58
 * @version1.0
 */
@Controller
@RequestMapping(value = "flow")
public class FlowController {
    /**
     * 日志类
     */
    private static final Logger log = LoggerFactory.getLogger(FlowController.class);
    /**
     * 流程处理服务
     */
    @Autowired
    private IFlowService flowService;

    /**
     * Flowable 任务处理服务
     */
    @Autowired
    private TaskService taskService;

    @RequestMapping("/create")
    @ResponseBody
    public Map<String,Object> createFlow(){
        Map<String,Object> res =new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        String flowPath ="C:\\Users\\gblfy\\Desktop\\Workflow\\flowablestudy\\flowablelech12\\src\\main\\resources\\processes\\测试BPMN模型3.bpmn20.xml";

        Map<String,Object> createRes = flowService.createFlow(flowPath);

        if (null == createRes){
            res.put("msg","创建流程失败");
            res.put("res","0");
            res.put("data",data);
            return res;
        }
        List<Process> processes =(List<Process>)createRes.get("processes");

        ArrayList<String> ids = new ArrayList<>();
        for (Process process :processes){
            ids.add(process.getId());
        }
        data.put("processKeys",ids);
        data.put("deployId",((Deployment)createRes.get("deployment")).getId());
        res.put("data",data);
        res.put("msg","创建流程成功");
        res.put("res","1");
        return res;
    }

    /**
     * 获取审批管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String userId) {
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
        for (Task task : tasks) {
        }
        System.out.println(tasks.toString());

        return tasks.toString();
    }
    @RequestMapping("/start")
    @ResponseBody
    public Map<String,Object> startFlow(@RequestBody @RequestParam(required = false) Map<String,String> paras){
        Map<String,Object> res =new HashMap<>();
        Map<String,String> data = new HashMap<>();

        if (MapUtils.isEmpty(paras)){
            res.put("msg","启动流程失败");
            res.put("res","0");
            res.put("data",data);
            return res;
        }

        String processKey = paras.get("processKey");
        if (StringUtils.isEmpty(processKey)){
            res.put("msg","启动流程失败");
            res.put("res","0");
            res.put("data",data);
            return res;
        }

        Map<String,Object> flowParas=new HashMap<>();
        flowParas.putAll(paras);
        ProcessInstance processInstance = flowService.strartFlow(processKey,flowParas);
        if (null == processInstance){
            res.put("msg","启动流程失败");
            res.put("res","0");
            res.put("data",data);
            return res;
        }
        data.put("processId",processInstance.getId());
        res.put("msg","启动流程成功");
        res.put("res","1");
        res.put("data",data);
        return res;
    }


    @RequestMapping(value = "processDiagram")
    public void genProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {
        flowService.genProcessDiagram(httpServletResponse,processId);
    }


    @RequestMapping("/complete")
    @ResponseBody
    public Map<String,Object> completeTask(@RequestBody @RequestParam(required = false) Map<String,String> paras){
        Map<String,Object> res =new HashMap<>();
        Map<String,String> data = new HashMap<>();

        if (MapUtils.isEmpty(paras)){
            res.put("msg","请输入任务参数");
            res.put("res","0");
            res.put("data",data);
            return res;
        }

        String taskId = paras.get("taskId");
        if (StringUtils.isEmpty(taskId)){
            res.put("msg","请输入任务ID");
            res.put("res","0");
            res.put("data",data);
            return res;
        }

        Map<String,Object> flowParas=new HashMap<>();
        flowParas.putAll(paras);
        boolean bok = flowService.completeTask(taskId,flowParas);

        if (bok){
            data.put("taskId",taskId);
            res.put("msg","启动流程成功");
            res.put("res","1");
        }
        else {
            data.put("taskId",taskId);
            res.put("msg","启动流程失败");
            res.put("res","0");
        }

        res.put("data",data);
        return res;
    }

}
